import uuid from "../../utils/uuid";
import EventBus from "../../utils/EventBus";

Page({
  data: {
    activeName: 'notice',
    dialogVisible: false,
    noticeList: [
      {
        key: uuid(),
        title: '活动通知',
        content: '恭喜获取20租币，请查收',
        date: '2023-04-01 14:30',
        status: 0
      },
      {
        key: uuid(),
        title: '免费得iPhone11',
        content: '恭喜获取20租币，请查收',
        date: '2023-04-01 14:30',
        status: 0
      },
      {
        key: uuid(),
        title: '这里的字符限制是十二个字符',
        content: '恭喜获取20租币，请查收',
        date: '2023-04-01 14:30',
        status: 0
      },
      {
        key: uuid(),
        title: '节日赠租金',
        content: '人人租祝您节日快乐，送88个租币人人租祝您节日快乐，送88个租币！！！',
        date: '2023-04-01 14:30',
        status: 0
      }
    ],
    orderList: [
      {
        key: uuid(),
        title: '订单已取消',
        content: '订单已成功取消，感谢您的关注',
        date: '2023-04-01 14:30',
        status: 0
      },
      {
        key: uuid(),
        title: '订单已取消',
        content: '订单已成功取消，感谢您的关注',
        date: '2023-04-01 14:30',
        status: 0
      },
      {
        key: uuid(),
        title: '订单已取消',
        content: '订单已成功取消，感谢您的关注',
        date: '2023-04-01 14:30',
        status: 0
      },
      {
        key: uuid(),
        title: '订单已取消',
        content: '订单已成功取消，感谢您的关注',
        date: '2023-04-01 14:30',
        status: 0
      },
      {
        key: uuid(),
        title: '订单已取消',
        content: '订单已成功取消，感谢您的关注',
        date: '2023-04-01 14:30',
        status: 0
      }
    ],
    items: [
      {
        title: '活动公告',
        badge: 0,
      },
      {
        title: '订单信息',
        badge: 0,
      },
    ],
    generateStatus: false
  },
  options: {
    observers: true,
  },
  observers: {
    'noticeList, orderList': function (noticeList, orderList) {
      this.updateNewNum(noticeList, orderList)
    }
  },

  onLoad() {
    this.updateNewNum(this.data.noticeList, this.data.orderList)
    EventBus.on('updateStatus', (type: string, key: string) => this.updateStatus(type, key))
    EventBus.on('removeByKey', (type: string, key: string) => this.removeByKey(type, key))
    this.generate('notice', 10)
    this.generate('order', 10)
  },

  updateNewNum(noticeList, orderList) {
    const newNotice = noticeList.filter((item) => item.status === 0).length
    const newOrder = orderList.filter((item) => item.status === 0).length
    this.setData({
      items: this.data.items.map((item) => {
        if (item.title === '活动公告') {
          item.badge = newNotice
        } else {
          item.badge = newOrder
        }
        return item
      })
    })
  },

  onChange(current) {
    this.setData({
      activeName: current === 0 ? 'notice' : 'order',
    });
    this.generate(this.data.activeName, 5)
  },
  delRead() {
    this.setData({
      dialogVisible: true,
    })
  },

  markRead() {
    const newObj = {}
    if (this.data.activeName === 'notice') {
      newObj['noticeList'] = this.data.noticeList.map((item) => {
        item.status = 1
        return item
      })
    } else {
      newObj['orderList'] = this.data.orderList.map((item) => {
        item.status = 1
        return item
      })
    }
    this.setData(newObj)
  },

  onRemove() {
    const newObj = {}
    if (this.data.activeName === 'notice') {
      newObj['noticeList'] = this.data.noticeList.filter((item) => item.status === 0)
    } else {
      newObj['orderList'] = this.data.orderList.filter((item) => item.status === 0)
    }
    this.setData({
      dialogVisible: false,
      ...newObj

    })
  },

  onCancel() {
    this.setData({
      dialogVisible: false
    })
  },

  updateStatus(type: string, key: string) {
    const newObj = {}
    if (type === 'notice') {
      newObj['noticeList'] = this.data.noticeList.map((item) => {
        if (item.key === key) {
          item.status = 1
        }
        return item
      })
    } else {
      newObj['orderList'] = this.data.orderList.map((item) => {
        if (item.key === key) {
          item.status = 1
        }
        return item
      })
    }
    this.setData({
      ...newObj
    })
  },

  removeByKey(type: string, key: string) {
    const newObj = {}
    if (type === 'notice') {
      newObj['noticeList'] = this.data.noticeList.filter((item) => item.key !== key)
    } else {
      newObj['orderList'] = this.data.orderList.filter((item) => item.key !== key)
    }
    this.setData({
      ...newObj
    })
  },

  generate(type: string, num: number) {
    const list = type === 'notice' ? this.data.noticeList : this.data.orderList
    if (this.data.generateStatus || list.length >= 20) return
    this.setData({
      generateStatus: true
    })
    const arr = []
    const newObj = {}
    if (type === 'notice') {
      for (let i = 0; i < num; i++) {
        arr.push({
          key: uuid(),
          title: '活动通知',
          content: '恭喜获取20租币，请查收',
          date: '2023-04-01 14:30',
          status: 0
        })
      }
      newObj['noticeList'] = [...this.data.noticeList, ...arr]
    } else {
      for (let i = 0; i < num; i++) {
        arr.push({
          key: uuid(),
          title: '订单已取消',
          content: '订单已成功取消，感谢您的关注',
          date: '2023-04-01 14:30',
          status: 0
        })
      }
      newObj['orderList'] = [...this.data.orderList, ...arr]
    }
    setTimeout(() => {
      this.setData({ generateStatus: false, ...newObj })
    }, 2000)
  },
  onReachBottom() {
    this.generate(this.data.activeName, 5)
  },
});
